package edu.uw.cs.cse461.sp12.OSConsoleApps;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import edu.uw.cs.cse461.sp12.DB461.DB461.DB461Exception;
import edu.uw.cs.cse461.sp12.OS.DDNSFullName;
import edu.uw.cs.cse461.sp12.OS.OS;
import edu.uw.cs.cse461.sp12.OS.SNetControl;
import edu.uw.cs.cse461.sp12.SNetDB.SNetDB461.Photo;
import edu.uw.cs.cse461.sp12.SNetDB.SNetDB461.PhotoRecord;
import edu.uw.cs.cse461.sp12.util.Log;

public class SNet implements OSConsoleApp {
	private static final String TAG = "SNetConsole";

	public SNet() {
	}
	
	@Override
	public void shutdown() throws Exception {
	}

	@Override
	public String appname() {
		return "snet";
	}

	@Override
	public void run() {
		try {
			// Eclipse doesn't support System.console()
			BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
			while (true) {
				try {
					System.out.println("Commands: contact fetchUpdates fetchPhoto members photos fixDB");
					
					System.out.print("Enter a command, or exit to exit: ");
					String command = console.readLine();
					if (command.equals("exit")) {
						break;
					} else if (command.equals("contact")) {
						contact(console);
					} else if (command.equals("fetchUpdates")) {
						fetchUpdates(console);
					} else if (command.equals("fetchPhoto")) {
						fetchPhoto(console);
					} else if (command.equals("members")) {
						members(console);
					} else if (command.equals("photos")) {
						photos(console);
					} else if (command.equals("fixDB")) {
						fixDB(console);
					} else {
						System.out.println("Invalid command: " + command);
					}
				} catch (Exception e) {
					System.out.println("ERROR: " + e.getMessage());
					//e.printStackTrace();
				}
			}
		} catch (Exception e) {
			Log.e(TAG, "run() caught exception: " + e.getMessage());
		}
	}

	private void fixDB(BufferedReader console) throws DB461Exception {
		System.out.println("Fixing DB");
		SNetControl.fixDB();
	}

	private void photos(BufferedReader console) throws Exception {
		System.out.println("Photo Commands: list add choose");
		System.out.print("Enter a command, or exit to exit: ");
		String command = console.readLine();
		if (command.equals("exit")) {
			return;
		} else if (command.equals("list")) {
			// List photos
			System.out.println("Listing photos:");
			for (PhotoRecord photo : SNetControl.getGallery().values()) {
				System.out.println("\t" + photo.toString());
			}
		} else if (command.equals("add")) {
			// Add a photo
			System.out.println("Set your personal photo:");
			
			System.out.print("Enter a file name of the photo, or exit to exit: ");
			String fileString = console.readLine();
			if (fileString.equals("exit")) {
				return;
			}
			File myPhotoFile = new File(OS.galleryDir(), fileString);
			SNetControl.setMyPhoto(myPhotoFile);
		} else if (command.equals("choose")) {
			// Choose a favorite photo
			System.out.println("Set your chosen photo:");

			System.out.print("Enter a hash of your chosen photo, or exit to exit: ");
			String hashString = console.readLine();
			if (hashString.equals("exit")) {
				return;
			}
			int chosenFileHash = Integer.parseInt(hashString);
			SNetControl.setChosenPhoto(chosenFileHash);
		} else {
			System.out.println("Invalid photo command: " + command);
		}
	}

	private void members(BufferedReader console) throws Exception {
		System.out.println("Member Commands: list friends befriend unfriend");
		System.out.print("Enter a command, or exit to exit: ");
		String command = console.readLine();
		if (command.equals("exit")) {
			return;
		}
		
		if (command.equals("list")) {
			// List all members
			System.out.println("Listing all members:");
			for (DDNSFullName member : SNetControl.getMembersList()) {
				System.out.println("\t" + member.getName());
			}
		} else if (command.equals("friends")) {
			// List friends
			System.out.println("Listing friends:");
			for (DDNSFullName friend : SNetControl.getFriendsList()) {
				System.out.println("\t" + friend.getName());
			}
		} else if (command.equals("befriend")) {
			System.out.print("Enter the new friend's name, or exit to exit: ");
			String name = console.readLine();
			if (name.equals("exit")) {
				return;
			}
			SNetControl.befriend(new DDNSFullName(name));
		} else if (command.equals("unfriend")) {
			System.out.print("Enter the existing friend's name, or exit to exit: ");
			String name = console.readLine();
			if (name.equals("exit")) {
				return;
			}
			SNetControl.unfriend(new DDNSFullName(name));
		}
	}

	private void fetchPhoto(BufferedReader console) throws Exception {
		System.out.print("Enter a host, or exit to exit: ");
		String host = console.readLine();
		if (host.equals("exit")) {
			return;
		}
		DDNSFullName hostFullName = new DDNSFullName(host);
		
		System.out.print("Enter a photo hash, or exit to exit: ");
		String photoHashString = console.readLine();
		if (photoHashString.equals("exit")) {
			return;
		}
		int photoHash = Integer.parseInt(photoHashString);
		
		SNetControl.fetchPhoto(hostFullName, photoHash);
	}

	private void fetchUpdates(BufferedReader console) throws Exception {
		System.out.print("Enter a host, or exit to exit: ");
		String host = console.readLine();
		if (host.equals("exit")) {
			return;
		}
		
		SNetControl.fetchUpdates(new DDNSFullName(host));
	}

	private void contact(BufferedReader console) throws Exception {
		System.out.print("Enter a host, or exit to exit: ");
		String host = console.readLine();
		if (host.equals("exit")) {
			return;
		}
		
		SNetControl.contact(new DDNSFullName(host));
	}
}
